package com.greatmay.webapp.action;

import java.util.List;

import com.greatmay.dao.SearchException;
import com.greatmay.model.Category;
import com.greatmay.model.ProductStatus;
import com.greatmay.model.Video;
import com.greatmay.service.GenericManager;
import com.greatmay.service.VideoService;
import com.opensymphony.xwork2.Preparable;

public class VideoAction extends BaseAction implements Preparable {
    private static final long serialVersionUID = 1982146716163316046L;
    private VideoService videoService;
    private GenericManager<Category, Long> categoryManager;
    private List<Category> categories;
    private List<Video> videos;
    private Video video;
    private Long id;
    private String query;

    public void setVideoService(VideoService videoManager) {
        this.videoService = videoManager;
    }

    public void setCategoryManager(GenericManager<Category, Long> categoryManager) {
        this.categoryManager = categoryManager;
    }

    public List<Video> getVideos() {
        return videos;
    }

    public List<Category> getCategories() {
        return categories;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String videoId = getRequest().getParameter("video.id");
            if (videoId != null && !videoId.equals("")) {
                video = videoService.get(new Long(videoId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
        try {
            videos = videoService.search(query, Video.class);
        } catch (SearchException se) {
            addActionError(se.getMessage());
            videos = videoService.getAll();
        }
        return SUCCESS;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Video getVideo() {
        return video;
    }

    public void setVideo(Video video) {
        this.video = video;
    }

    public String delete() {
        videoService.remove(video.getId());
        saveMessage(getText("video.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            video = videoService.get(id);
        } else {
            video = new Video();
            video.setStatus(ProductStatus.NORMAL);
        }
        categories = categoryManager.getAll();
        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (video.getId() == null);

        videoService.save(video);

        String key = (isNew) ? "video.added" : "video.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}